﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;

using Ext.Net;
using BG.Data;
using Kwan.Helper.Strings;
using Kwan.Data;
using BG.Service;

namespace BG.Web.Admin.User
{
    public partial class Default : UIPage
    {
        IList<TRole> RoleList = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            BtnAddInfo.Hidden = !permit[0];
            BtnChangeScale.Hidden = !permit[2];

            BtnDeleteInfo.Hidden = !permit[3];
            if (permit[3])
            {
                List<GridCommand> gridCommand = new List<GridCommand>{
                        ExtHelper.CreateGridCommand("Delete", Icon.Delete, "删除", "删除该条记录")
                    };
                GridPanel1.ColumnModel.Columns.Add(ExtHelper.CreateCommandColumn(gridCommand));
            }
        }

        protected void BtnAddInfo_Click(object sender, EventArgs e)
        {
            SetEditWindow(new VUser());
            EditWindow.Show();
        }

        [DirectMethod]
        public void DeleteSelected(string obj)
        {
            string[] list = obj.Split(',');
            for (int i = 0; i < list.Length; i++)
                (TUser.entityID == list[i]).Edit<TUser>(a => a.Scale = 0);
                //CrudManage.DeleteSelected(obj, "BG_User");
            Store1.DataBind();
        }

        [DirectMethod]
        public void ChangeState(string obj, string NameValue)
        {
            IList<TUser> list = JSON.Deserialize<IList<TUser>>(obj);
            if (list.Count > 0)
            {
                CrudManage.ChangeState(list, NameValue);
                Store1.DataBind();
            }
            else
            {
                ExtHelper.GetNotification("系统提示", "请至少选择一项！").Show();
            }
        }

        [DirectMethod]
        public void EditInfo(string id)
        {
            if (!permit[2]) return;
            SetEditWindow((VUser.entityID == id).ListFirst<VUser>());
            EditWindow.Show();
        }

        protected void RefreshDS(object sender, StoreRefreshDataEventArgs e)
        {
            NameValueCollection ps = new NameValueCollection();
            ps.Set("gridsort", e.Sort);
            ps.Set("dir", e.Dir.ToString());
            
            //搜索项
            ps.Set("UserName", txtSearch.Text);

            int pageindex = e.Start / e.Limit;
            Kwan.Data.Region r = new Kwan.Data.Region(pageindex, e.Limit);

            Expression exp = UserQuery.MakeSearchWhere(ps);
            exp = exp & TUser.scale > 0;
            IList<VUser> ListView = exp.List<VUser>(r, new string[] { e.Sort + " " + e.Dir });

            e.Total = new Expression().Count<TUser>();
            Store1.DataSource = ListView;
            Store1.DataBind();
        }

        protected void RefreshRoleDS(object sender, StoreRefreshDataEventArgs e)
        {
            RoleDS.DataSource = new Expression().List<TRole>();
            RoleDS.DataBind();
        }

        protected void NewEditWindow()
        {
            txtID.Text = "0";
            //txtScale.Value = "1";
            //txtEmail.Text = string.Empty;
            //txtLogo.Text = string.Empty;
            txtRoleID.SetValue(string.Empty, string.Empty);
			txtUserName.Text = string.Empty;
        }

        protected void SetEditWindow(VUser tt)
        {
            txtID.Text = tt.EntityID;
            txtName.Text = tt.Name;
            //txtScale.Value = tt.Scale;
            //txtEmail.Text = tt.Email;
            //txtLogo.Text = tt.Logo;
            if (!string.IsNullOrEmpty(tt.RoleID))
                txtRoleID.SetValue(tt.RoleID.ToString(), tt.RoleNameCN + "(" + tt.RoleNameEN + ")");
            else
                txtRoleID.SetValue("", "");
			txtUserName.Text = tt.UserName;
            txtPassword.Text = string.IsNullOrEmpty(tt.Password) ? "" : MD5.Md5Decrypt(tt.Password);
        }

        protected void SaveEditWindow(object sender, DirectEventArgs e)
        {
            TUser tt = string.IsNullOrEmpty(txtID.Text) ? new TUser() :
                (TUser.entityID == txtID.Text).ListFirst<TUser>();

            //tt.Email = txtEmail.Text;
            //tt.Logo = txtLogo.Text;
            tt.RoleID = txtRoleID.Value.ToString();
            tt.UserName = txtUserName.Text;
            tt.Name = txtName.Text;
            tt.Scale = 1;
            tt.Password = MD5.Md5Encrypt(txtPassword.Text);

            DBContext.Save(tt);
            Store1.DataBind();
            EditWindow.Hide();
        }

        protected void GetExamplesNodes(object sender, NodeLoadEventArgs e)
        {
            if (e.NodeID == "root")
            {
                RoleList = new Expression().List<TRole>();
                e.Nodes = CreateTree(RoleList);
            }
        }

        private TreeNodeCollection CreateTree(IList<TRole> list)
        {
            TreeNodeCollection nodes = new TreeNodeCollection(false);
            foreach (TRole t in list)
            {
                TreeNode node = new TreeNode();
                node.NodeID = t.EntityID.ToString();
                node.Text = t.NameEN + "(" + t.NameCN + ")";
                nodes.Add(node);
            }
            return nodes;
        }
    }
}